package edu.zjut.hjg.saas.web.controller.home;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import edu.zjut.hjg.saas.biz.service.ActivityService;

@Controller
@RequestMapping("/home/activity.htm")
public class ActivityController {
    private static final int PAGE_SIZE = 10;

    @Value("${saas.web.app.server.port}")
    private int              appServerPort;

    @Autowired
    private ActivityService  activityService;

    @RequestMapping
    public void activity(
                         @RequestParam(value = "page", required = false, defaultValue = "1") int page,
                         Model model) {
        if (page < 1) {
            page = 1;
        }
        model.addAttribute("port", (appServerPort == 80 ? "" : ":" + appServerPort));
        model.addAttribute("paginated", activityService.listAll(PAGE_SIZE, page));
    }
}
